/**
 * Created by wry on 2015/4/13.
 */
var Users = mongoose.model('user');
var async = require('async');
var neighbourActivity = mongoose.model('activity_neighbour');
var mysqlUtil = require('../../common/utils/mysqlUtil');
var Question = mongoose.model('question');
var Community = mongoose.model('community');

Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length == 1 ? o[k] :
                ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
if (!String.prototype.format) {
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
                ? args[number]
                : match
                ;
        });
    };
}
function commentToString(f) {
    return f.toString().
        replace(/^[^\/]+\/\*!?/, '').
        replace(/\*\/[^\/]+$/, '');
}
function sqlConditionString(conditionList) {
    var len = conditionList.length
    if (len == 0)
        return ''
    var condition = "where "

    for (var i = 0; i < len; i++) {
        condition += conditionList[i]
        if (i != len - 1)
            condition += ' and '
    }
    return condition
}

exports.getTopUsersService = function (community, numPerPage, filter, callback) {

    var commCondition = ""
    if (community != 'all')
        commCondition = "and rank.community='{0}'".format(community)

    var sql = commentToString(function () {
        /*
         SELECT rank.*, u.nickName, convert(rank.dateCreated, char) timeCreated
         FROM 66xiaoqu_rec.user_ranking rank, 66xiaoqu_rec.user u
         where rank.uid = u._id
         {0}
         order by rank.{2} desc
         limit {1}
         */
    }).format(commCondition, numPerPage, filter)
    mysqlUtil.queryBI(sql, function (err, result) {
        callback(err, result)
    })
}

exports.getTopQuestionsService = function (community, numPerPage, filter, callback) {

    var commCondition = ""
    if (community != 'all')
        commCondition = "and q.community='{0}'".format(community)

    var sql = commentToString(function () {
        /*
         select q.community, communityName, category, q._id, q.`desc`, q.browserCount, count(*) replyCount, convert(q.dateCreated, char) dateCreated FROM
         66xiaoqu_rec.question q, 66xiaoqu_rec.question_replies r, 66xiaoqu_rec.community c
         where q._id = r.question_id and q.community = c._id
         {0}
         group by q._id
         order by {2} desc
         limit {1}
         */
    }).format(commCondition, numPerPage, filter)
    mysqlUtil.queryBI(sql, function (err, result) {
        callback(err, result)
    })
}

exports.getTopActivitiesService = function (community, numPerPage, filter, callback) {

    var commCondition = ""
    if (community != 'all')
        commCondition = "and act.community='{0}'".format(community)

    var sql = commentToString(function () {
        /*
         SELECT act.community, communityName, act._id actId, title, partakeNum, browserCount, count(*) replyCount, convert(dateCreated, char) dateCreated
         FROM 66xiaoqu_rec.activity_neighbour act, 66xiaoqu_rec.activity_neighbour_replies re, 66xiaoqu_rec.community comm
         where act._id = re.activity_neighbour_id and act.community = comm._id
         {0}
         group by act._id
         order by {2} desc
         limit {1}
         */
    }).format(commCondition, numPerPage, filter)
    mysqlUtil.queryBI(sql, function (err, result) {
        callback(err, result)
    })
}
